package com.zc.im.service.im.group.impl;

import com.zc.im.mapper.im.group.ZcImGroupAuditMapper;
import com.zc.im.mapper.im.group.ZcImGroupMapper;
import com.zc.im.mapper.im.group.ZcImGroupMemberMapper;
import com.zc.im.model.im.group.*;
import com.zc.im.service.im.group.ImGroupAuditService;
import com.zc.im.service.im.group.ImGroupMemberService;
import com.zc.im.service.im.group.ImGroupService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupAuditServiceImpl implements ImGroupAuditService {

    @Autowired
    private ZcImGroupAuditMapper groupAuditMapper;

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private ImGroupMemberService imGroupMemberService;


    private static Logger logger = Logger.getLogger(ImGroupAuditServiceImpl.class);


    /**
     * 添加审批
     * @param groupAudit
     * @return
     */
    @Override
    public Integer insertGroupAudit(ZcImGroupAudit groupAudit) {
        Integer groupAnditId = null;
        if (groupAudit == null) {
            logger.info("insert GroupAndit  is null ");
            return groupAnditId;
        }
        try {
            groupAudit.setStatus(ImGolbalTypeContext.STATUS_VALID);
            groupAudit.setCreatedTime(new Date());
            groupAnditId = groupAuditMapper.insertSelective(groupAudit);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupAnditId;
    }

    /**
     * 更新审批
     * @param groupAudit
     * @return
     */
    @Override
    public Integer updateGroupAudit(ZcImGroupAudit groupAudit) {
        Integer groupAnditId = null;
        if (groupAudit == null) {
            logger.info("update GroupAndit  is null ");
            return groupAnditId;
        }
        try {
            groupAudit.setUpdatedTime(new Date());
            ZcImGroupAuditExample example = new ZcImGroupAuditExample();
            ZcImGroupAuditExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(groupAudit.getId());

            groupAnditId = groupAuditMapper.updateByExample(groupAudit, example);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupAnditId;

    }


    /**
     * 删除审批
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupAudit(Integer id) {
        Integer groupAnditId = null;
        if (id == null) {
            logger.info("delete GroupAndit  is null ");
            return groupAnditId;
        }
        try {
            ZcImGroupAuditKey auditKey = new ZcImGroupAudit();
            auditKey.setId(id);

            groupAnditId = groupAuditMapper.deleteByPrimaryKey(auditKey);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupAnditId;
    }

    /**
     * 通过id type groupName created_user_id audit_user_id  查询审批
     * @param zcImGroupAnditModel
     * @return
     */
    @Override
    public ZcImGroupAudit queryGroupAudit(ZcImGroupAudit zcImGroupAnditModel) {
        ZcImGroupAudit imGroupAudit = null;
        if (zcImGroupAnditModel == null) {
            logger.info("query GroupAndit is null");
            return imGroupAudit;
        }
        try {
            ZcImGroupAuditExample example = new ZcImGroupAuditExample();
            ZcImGroupAuditExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(zcImGroupAnditModel.getId());


            List<ZcImGroupAudit> groupAudits = groupAuditMapper.selectByExample(example);
            if(groupAudits == null || groupAudits.size() == 0){
                return imGroupAudit;
            }
            imGroupAudit = groupAudits.get(0);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return imGroupAudit;
    }


    /**
     * 审批群组
     * 2017-7-11 14:03:15
     * @param groupAudit
     * @return
     */
    public Integer auditGroup(ZcImGroupAudit groupAudit){
        Integer updateStatus = 0;
        try {
            if(groupAudit == null){
                return updateStatus;
            }
            if (groupAudit.getAuditStatus().equals(ImGolbalTypeContext.AUDIT_PASS)){
                //更新群组审批表
                updateStatus = updateGroupAudit(groupAudit);
                //添加群组
                ZcImGroup imGroup = insertGroup(groupAudit);
                //添加群组成员
                insertGroupMember(groupAudit, imGroup.getId());
            }else if(groupAudit.getAuditStatus().equals(ImGolbalTypeContext.AUDIT_REFUSE)) {
                //更新群组审批表
                updateStatus = updateGroupAudit(groupAudit);
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return updateStatus;
    }


    public ZcImGroup insertGroup(ZcImGroupAudit groupAudit){
        ZcImGroup zcImGroup = new ZcImGroup();
        zcImGroup.setObjId(groupAudit.getCompanyId());
        zcImGroup.setType(ImGolbalTypeContext.IM_GROUP_TYPE_COMPANY);
        zcImGroup.setGroupName(groupAudit.getGroupName());
        zcImGroup.setAdminUserId(groupAudit.getCreatedUserId());
        zcImGroup.setStatus(ImGolbalTypeContext.STATUS_VALID);
        imGroupService.insertGroup(zcImGroup);
        zcImGroup.setId(zcImGroup.getId());
        return zcImGroup;
    }

    public ZcImGroupMember insertGroupMember(ZcImGroupAudit groupAudit, Integer groupId){
        //把创建管理员加入群内
        ZcImGroupMember zcImGroupMember = new ZcImGroupMember();
        //加入公司id
        zcImGroupMember.setCompanyId(groupAudit.getCompanyId());
        //群加入成员
        zcImGroupMember.setUserId(groupAudit.getCreatedUserId());
        //加入群id
        zcImGroupMember.setGroupId(groupAudit.getId());

        //加入业务状态
        zcImGroupMember.setUserStatus(ImGolbalTypeContext.STATUS_VALID);
        //加入状态
        imGroupMemberService.insertGroupMember(zcImGroupMember);
        zcImGroupMember.setId(zcImGroupMember.getId());
        return zcImGroupMember;
    }

}
